Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The `destr` npm package is designed to safely parse JSON strings without throwing an error for invalid JSON. It can return the original string if parsing fails, making it useful for handling dynamic JSON data that may not always be properly formatted. It also recognizes and correctly parses values like `null`, `true`, `false`, and `undefined`.
Safe JSON parsing
Safely parse a JSON string without throwing an error. If the string is not valid JSON, it returns the original string.
"const destr = require('destr');
const json = '{\"key\":\"value\"}';
const parsed = destr(json);
console.log(parsed); // Output: { key: 'value' }"
Parsing special JSON values
Correctly parse special JSON values such as `null`, `true`, `false`, and `undefined`, returning their corresponding JavaScript types.
"const destr = require('destr');
console.log(destr('null')); // Output: null
console.log(destr('true')); // Output: true
console.log(destr('false')); // Output: false
console.log(destr('undefined')); // Output: undefined"
Similar to `destr`, `json5` allows for parsing of JSON data with more lenient syntax rules, such as trailing commas and comments. However, `json5` focuses on extending JSON syntax to be more flexible, while `destr` focuses on safe parsing and handling special values.
This package offers functionality similar to `destr` by providing a safe way to parse JSON strings without throwing errors for invalid JSON. The main difference is in the API and specific handling of non-JSON values.
A faster, secure and convenient alternative for JSON.parse
.
Install dependency:
# npm
npm i destr
# yarn
yarn add destr
# pnpm
pnpm i destr
Import into your Node.js project:
// ESM
import { destr, safeDestr } from "destr";
// CommonJS
const { destr, safeDestr } = require("destr");
import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts";
console.log(destr('{ "deno": "yay" }'));
const obj = JSON.parse("{}"); // obj type is any
const obj = destr("{}"); // obj type is unknown by default
const obj = destr<MyInterface>("{}"); // obj is well-typed
// Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse();
// undefined
destr();
// Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse("TRUE");
// true
destr("TRUE");
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
JSON.parse("salam");
// "salam"
destr("salam");
Note: This fails in safe/strict mode with safeDestr
.
const input = '{ "user": { "__proto__": { "isAdmin": true } } }';
// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input);
// { user: {} }
destr(input);
When using safeDestr
it will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)
// Returns "[foo"
destr("[foo");
// Throws an error
safeDestr("[foo");
destr
is faster generally for arbitrary inputs but also sometimes little bit slower than JSON.parse
when parsing a valid JSON string mainly because of transform to avoid prototype pollution which can lead to serious security issues if not being sanitized. In the other words, destr
is better when input is not always a JSON string or from untrusted source like request body.
Check Benchmark Results or run with pnpm run bench:node
or pnpm run bench:bun
yourself!
MIT. Made with 💖
FAQs
A faster, secure and convenient alternative for JSON.parse
We found that destr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.